package org.teach.study.boot;

import java.util.Arrays;
import java.util.List;

import org.quincy.rock.core.vo.PageSet;
import org.quincy.rock.core.vo.Result;
import org.quincy.rock.core.vo.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <b>SimpleController。</b>
 * <p><b>详细说明：</b></p>
 * <!-- 在此添加详细说明 -->
 * 无。
 * 
 * @version 1.0
 * @author mex2000
 * @since 1.0
 */
@Slf4j
public class SimpleController<T extends Entity, S extends Service<T>> extends BaseController<T, S> {

	@ApiOperation(value = "查询多个实体", notes = "该接口继承自SimpleController")
	@ApiImplicitParam(name = "id", value = "多个主键id", required = true, dataType = "long", allowMultiple = true)
	@RequestMapping(value = "/queryMore", method = { RequestMethod.GET })
	public @ResponseBody Result<List<T>> queryMore(@RequestParam("id") Long[] ids) {
		log.debug("call queryMore");
		List<T> list = this.getService().findMore(Arrays.asList(ids));
		return Result.toResult(list);
	}

	@ApiOperation(value = "查询所有实体", notes = "该接口继承自SimpleController")
	@ApiImplicitParam(name = "sort", value = "排序规则字符串")
	@RequestMapping(value = "/queryAll", method = { RequestMethod.GET })
	public @ResponseBody Result<List<T>> queryAll(String sort) {
		log.debug("call queryAll");
		List<T> list = this.getService().findAll(Sort.parse(sort));
		return Result.toResult(list);
	}

	@ApiOperation(value = "分页查询", notes = "该接口继承自SimpleController")
	@ApiImplicitParams({ @ApiImplicitParam(name = "sort", value = "排序规则字符串"),
			@ApiImplicitParam(name = "pageNumber", value = "页码", required = true, dataType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataType = "int") })
	@RequestMapping(value = "/queryPage", method = { RequestMethod.GET })
	public @ResponseBody Result<PageSet<T>> queryPage(String sort, @RequestParam() int pageNumber,
			@RequestParam int pageSize) {
		log.debug("call queryPage");
		PageSet<T> ps = this.getService().findPage(Sort.parse(sort), pageNumber, pageSize);
		return Result.toResult(ps);
	}

	@ApiOperation(value = "根据指定的属性名和值返回一条数据", notes = "该接口继承自SimpleController")
	@ApiImplicitParams({ @ApiImplicitParam(name = "propName", value = "属性名", required = true),
			@ApiImplicitParam(name = "propValue", value = "属性值", required = true) })
	@RequestMapping(value = "/queryByName", method = { RequestMethod.GET })
	public @ResponseBody Result<T> queryByName(@RequestParam String propName, @RequestParam Object propValue) {
		log.debug("call queryByName");
		T vo = this.getService().findByName(propName, propValue);
		return Result.toResult(vo);
	}

	@ApiOperation(value = "根据指定的属性名和值返回符合条件的所有数据", notes = "该接口继承自SimpleController")
	@ApiImplicitParams({ @ApiImplicitParam(name = "propName", value = "属性名", required = true),
			@ApiImplicitParam(name = "propValue", value = "属性值", required = true),
			@ApiImplicitParam(name = "sort", value = "排序规则字符串") })
	@RequestMapping(value = "/queryAllByName", method = { RequestMethod.GET })
	public @ResponseBody Result<List<T>> queryAllByName(@RequestParam String propName, @RequestParam Object propValue,
			String sort) {
		log.debug("call queryAllByName");
		List<T> list = this.getService().findAllByName(propName, propValue, Sort.parse(sort));
		return Result.toResult(list);
	}
}
